/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ainlolcat.ainscience.matrix;

import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.column.ColumnImpl;

import java.util.*;

/**
 *
 * @author ain
 */
public class Matrix {

    Map<Integer,Column> data = new Hashtable();

    public Set<Integer> getKeys(){
        return data.keySet();
    }

    /*
     * can return null if value unspecified
     * Note: ALWAYS return CLONE
     */
    public Column getColumn(int num){
        if (data.get(num)==null) return null;
        return data.get(num).clone();
    }

    public int getColumnCount(){
        return data.size();
    }

    public int getRowCount(){
        if (data.isEmpty()) return 0;
        return data.values().iterator().next().size();
    }

    private Column getInnerColumn(int num){
        return data.get(new Integer(num));
    }

    /*
     * rewrite data if exist !!
     */
    public boolean setColumn(int num, Column value){
        data.put(num, value);
        return true;
    }

    public boolean addData(int num, String value){
        if (getInnerColumn(num)!=null){
            getInnerColumn(num).addValue(value);
            return true;
        }
        else{
            List newData = new ArrayList();
            newData.add(value);
            return setColumn(num,new ColumnImpl(newData));
        }
    }

    public void setCaption(int num, String value){
        if (getInnerColumn(num)==null){
            Column temp = new ColumnImpl(value);
            setColumn(num,temp);
        }
        else{
            getInnerColumn(num).setName(value);
        }
    }

    public boolean removeLastRow(){
        Set<Integer> keys = data.keySet();
        for (Integer key:keys){
            data.get(key).removeLast();
        }
        return true;
    }
}
